<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>修改管理员信息</title>
<link rel="stylesheet" href="../../bootstrap/css/bootstrap.min.css">
<script type="text/javascript" src="../../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/bootbox.min.js"></script>
</head>
<body class="container">
	<div class=" panel panel-warning ">
		<div class="panel-heading">
			<h3 class="panel-title">修改管理员信息</h3>
		</div>
		<div class="panel-body">
			<form id="upForm" class="form-horizontal">
				<div class="form-group">
					<label class="col-xs-2 control-label">用户名</label>
					<div class="col-xs-10">
						<input type="text" class="form-control" name="username" readonly>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-2 control-label">密码</label>
					<div class="col-xs-10">
						<input type="password" class="form-control" name="password" placeholder="密码" autofocus required>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-2 control-label">姓名</label>
					<div class="col-xs-10">
						<input type="text" class="form-control" name="name" placeholder="姓名" required>
					</div>
				</div>

				<div class="form-group">
					<label class="col-xs-2 control-label">权限</label>
					<div class="col-xs-10">
						<div class="col-xs-6 radio">
							<label> <input type="radio" name="qx" id="opQx1" value="1" checked>普通管理员
							</label>
						</div>
						<div class="col-xs-6 radio">
							<label> <input type="radio" name="qx" id="opQx2" value="2"> 高级管理员
							</label>
						</div>
					</div>
				</div>

				<div class="form-group">
					<button id="btnReg" class="btn btn-info btn-block" type="submit">修改</button>
				</div>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		$(function() {
			
			var qx;
			$.ajax({
				url : "../../adminsession",
				async : false,
				type : "GET"
			}).done(function(result) {
				qx = result;
			});
			if (qx == "0") {
				window.location.href = "../noauthority.html";
				return;
			}
			
			function getQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = window.location.search.substr(1).match(reg);
				if (r != null)
					return unescape(r[2]);
				return null;
			}
			var username = getQueryString("username");
			var id;
			$.ajax({
				url : "../../admin/" + username,
				type : "GET"
			}).done(
					function(admin) {
						id = admin.id;
						$("input[name=username]").val(admin.username);
						$("input[name=password]").val(admin.password);
						$("input[name=name]").val(admin.name);
						$("input[name=qx][value=" + admin.qx + "]").attr(
								"checked", true);
					});

			$("#upForm").submit(function() {
				var username = $("input[name=username]").val();
				var password = $("input[name=password]").val();
				var name = $("input[name=name]").val();
				var qx = $("input[name=qx]:checked").val();
				var admin = {
					id : id,
					username : username,
					password : password,
					name : name,
					qx : qx
				};
				$.ajax({
					url : "../../admin",
					type : "PUT",
					data : JSON.stringify(admin),
					headers : {
						"Content-Type" : "application/json",
						"charset" : "utf-8"
					}
				}).done(function(result) {
					if (result == 'success') {
						window.location.href = "../upsuccess.html";
					} else {
						bootbox.alert(result);
					}
				});
				return false;
			});
		});
	</script>
</body>
</html>